package com.one.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.one.bean.ConsigneeInfo;
import com.one.bean.Orders;
import com.one.config.*;
import com.one.service.QueryOrderService;
import com.one.util.HttpClient4Util;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ShiWen
 * @date 2022/1/18 17:36
 * @Version 1.0
 * @Description TODO 快递服务接口
 */
@RestController
@Slf4j
public class ExpressController {
    @Autowired
    private KdApiEOrderDemo kdApiEOrderDemo;
    /**
     *  发件人信息
     */
    @Autowired
    private Sender sender;
    @Autowired
    private QueryOrderService orderService;
    @Autowired
    private ExpressConfig expressConfig;
    /**
     *  快递下单发货
     */
    @PostMapping("/orderExpress")
    public RequestReturn<String> orderExpress(String orderNo) throws Exception {
        log.info("接收 的 订单号：{}",orderNo);
        /**
         *  创建请求链接
         */
        String expressUrl =expressConfig.getOrderExpressUrl();
        // 查询订单信息
        OrderVo orderVo = orderService.queryOrderByNo(orderNo);
        log.info("查询的订单信息：{}",orderVo);
        // 请求参数
        Map<String,Object> RequestData = new HashMap<>();

        // 快递公司
        RequestData.put("ShipperCode",expressConfig.getShipperCode());
        RequestData.put("MonthCode",expressConfig.getMonthCode());
        RequestData.put("CustomerName",expressConfig.getCustomerName());
        RequestData.put("ExpType",expressConfig.getExpType()); //快递业务类型
        RequestData.put("PayType",expressConfig.getPayType()); //运费支付方式
        RequestData.put("orderCode",orderVo.getOrderNo());
        /**
         *  发件人信息
         */
        RequestData.put("Sender",sender);
        RequestData.put("Quantity",1);
        //商品信息
        List<Commodity> commodities = new ArrayList<>();
        commodities.add(Commodity.builder().GoodsName(orderVo.getProductName()).GoodsQuantity(orderVo.getProdNum()).build());
        RequestData.put("Commodity",commodities);
        /**
         *  收件人信息
         *  private PersonInfo Receiver;
         */
        // 收货信息
        ConsigneeInfo consigneeInfo = orderVo.getConsigneeInfo();
        RequestData.put("Receiver",PersonInfo.builder().Name(consigneeInfo.getName())
                .ProvinceName(consigneeInfo.getProvinces()).CityName(consigneeInfo.getCity())
                .ExpAreaName(consigneeInfo.getCounty()).Address(consigneeInfo.getStreet())
                .Tel(consigneeInfo.getPhone()).build());
        log.info("应用级参数：{}",JSON.toJSONString(RequestData));
        // 组装系统参数
        Map<String, Object> paramMap = kdApiEOrderDemo.getParamMap(JSON.toJSONString(RequestData),"1007");
        log.info("最终参数：{}",paramMap);
        String response4PostByMap = HttpClient4Util.getResponse4PostByMap(expressUrl, paramMap, "utf-8");
        JSONObject jsonObject = JSON.parseObject(response4PostByMap);
        log.info("返回的参数：{}",jsonObject);
        if (Boolean.parseBoolean(jsonObject.get("Success").toString())){
            // 成功 插入快递号到订单中...
            orderService.insertExpressNo(orderNo,jsonObject.getJSONObject("Order").getString("LogisticCode"));
        }

        return RequestReturnFactory.success(jsonObject.getJSONObject("Order").getString("LogisticCode"));
    }
    /**
     * @description 快递查询
     * @author ShiWen
     * @date 2022/1/19 8:39
     */
    @PostMapping("/selectExpress")
    public RequestReturn<Map<String,Object>> selectExpress(String LogisticCode,String orderNo) throws Exception {
        log.info("接收 的 订单号：{}",orderNo);
        String expressUrl =expressConfig.getOrderExpressUrl();
        // 请求参数
        Map<String,Object> RequestData = new HashMap<>();

        // 快递公司
        RequestData.put("ShipperCode",expressConfig.getShipperCode());
        RequestData.put("OrderCode",orderNo);
        RequestData.put("LogisticCode",LogisticCode);
        Map<String, Object> paramMap = kdApiEOrderDemo.getParamMap(JSON.toJSONString(RequestData),"1002");
        log.info("最终参数：{}",paramMap);
        String response4PostByMap = HttpClient4Util.getResponse4PostByMap(expressUrl, paramMap, "utf-8");
        Map<String,Object> map = JSON.parseObject(response4PostByMap, HashMap.class);
        return RequestReturnFactory.success(map);
    }
}
